package com.atguigu.dga.governance.assessor.storage;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.governance.assessor.Assessor;
import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import com.atguigu.dga.meta.bean.TableMetaInfo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName: TableSimilarAssessor
 * Package: com.atguigu.dga.governance.assessor
 * Description:
 *
 * @Author:
 * @Create: 2024/3/16 14:43
 * @Version: 1.0
 */
@Component("TABLE_SIMILAR")
public class TableSimilarAssessor extends Assessor {
    @Override
    public void checkProblem(AssessParam assessParam, GovernanceAssessDetail governanceAssessDetail) {
        // TODO 0 考评参数获取
        String metricParamsJson = assessParam.getGovernanceMetric().getMetricParamsJson();
        JSONObject jsonObject = JSON.parseObject(metricParamsJson, JSONObject.class);
        Integer paramPercent = jsonObject.getInteger("percent");

        // TODO 1 当前表 名字 层级
        String currTableName = assessParam.getTableMetaInfo().getTableName();
        String currDwLevel = assessParam.getTableMetaInfo().getTableMetaInfoExtra().getDwLevel();

        // TODO 2 当前表的 字段名集合
        List<String> currTableColNameList = getColNameList(assessParam.getTableMetaInfo());

        // 维护相似表的集合
        ArrayList<String> similarTableNameList = new ArrayList<>();

        // TODO 3 判断同层次的表 （需要拿到所有表 可以去查库，通过assessParam上层doAssess穿过来）
        for (TableMetaInfo tableMetaInfo : assessParam.getTableMetaInfoList()) {
            // 排除非同层次和自己
            if(currTableName.equals(tableMetaInfo.getTableName()) ||
            !currDwLevel.equals(tableMetaInfo.getTableMetaInfoExtra().getDwLevel())) {
                continue;
            }
            // 获取同层次表的字段名集合
            List<String> otherTableColNameList = getColNameList(tableMetaInfo);

            // 当前表 与 同层次表 字段名集合取交集
            Collection similarColNameList = CollectionUtils.intersection(currTableColNameList, otherTableColNameList);

            if(similarColNameList.size() * 100  / currTableColNameList.size() > paramPercent) {
                // 与当前表 字段名 重复 超过参数的 表 维护起来 给分之后给一下备注comment
                similarTableNameList.add(tableMetaInfo.getTableName());
            }
        }

        // TODO 4 判断是否存在相似表
        if(similarTableNameList.size() > 0) {
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            governanceAssessDetail.setAssessProblem("存在相似表");
            governanceAssessDetail.setAssessComment("相似表为：" + similarTableNameList);
        }


    }

    // TODO 2 处理成字段名集合
    private List<String> getColNameList(TableMetaInfo tableMetaInfo) {
        String colNameJson = tableMetaInfo.getColNameJson();
        List<JSONObject> colJsonObjArr = JSON.parseArray(colNameJson, JSONObject.class);

        // 处理成字段名集合
        List<String> currTableColList = colJsonObjArr.stream().
                map(jsonObj -> jsonObj.getString("name")).collect(Collectors.toList());

        return currTableColList;
    }
    //    @Override
//    public void doAssess() {
//        System.out.println("simil");
//    }
}
